/*************************************************************
*
* M3D_CTRL.CPP
*
*************************************************************/
#include <m3d/m3d_stdafx.h>

#include <ap_comm.h>
#include <m3d.h>
#include <m3d/m3d_ctrl.h>

/*************************************************************
* m3dCTRL_TIME::AdvanceTime()
*
*************************************************************/
void m3dCTRL_TIME::Update (float elapsedTime)
{
   if (IsEnabled()) {
      timeCur += elapsedTime * speed;
      if (timeCur > period) {
         if (!state.Is(M3D_CTRL_ST_NON_PERIODIC)) {
            timeCur = timeCur - period;
         }
         if (state.Is(M3D_CTRL_ST_DISABLE_AT_END)) {
            Enable(FALSE);
         }
      }
   }
   return;
}

/*************************************************************
* m3dCTRL_1D_SPL::GetValue()
*
*************************************************************/
void m3dCTRL_1D_SPL::GetValue (float *_val)
{
   pSpl->GetValue(GetCoeff(), _val);
   return;
}

/*************************************************************
* m3dCTRL_1D_SPL::GetMaxValue()
*
*************************************************************/
float m3dCTRL_1D_SPL::GetMaxValue (void)
{
   int      i, nKP = pSpl->NKp();
   float    val, valMax;

   valMax = -M3D_INFINITY;
   for (i = 0; i < nKP; i++) {
      //pSpl->GetValue((float)i, &val);
      pSpl->GetKpValue(i, &val);
      if (val > valMax) {
         valMax = val;
      }
   }
   return valMax;
}

/*************************************************************
* m3dCTRL_3D_SPL::GetValue()
*
*************************************************************/
void m3dCTRL_3D_SPL::GetValue (m3dV *_val)
{
   // get point on spline
   pSpl->GetValue(GetCoeff(), _val);
   return;
}
/*************************************************************
* m3dCTRL_COLOR_SPL::GetValue()
*
*************************************************************/
void m3dCTRL_COLOR_SPL::GetValue (m3dCOLOR *val)
{
   // get color from spline
   pSpl->GetValue(GetCoeff(), val);
   return;
}

/*************************************************************
* m3dCTRL_1D_NOISE::GetValue()
*
*************************************************************/
void m3dCTRL_1D_NOISE::GetValue (float *_val)
{
   m3dV  p;

   p.x = M3D_PI + freq * GetTimeCur();
   p.y = p.z = M3D_PI/2.f;
   *_val = minVal + ((maxVal - minVal) * (1.f + pNoise->GetValue(&p)) / 2.f);
   return;
}

/*************************************************************
* m3dCTRL_1D_NOISE::GetValue()
*
*************************************************************/
float m3dCTRL_1D_NOISE::GetMaxValue (void)
{
   return maxVal;
}

/*************************************************************
* m3dCTRL_COLOR_NOISE::GetValue()
*
*************************************************************/
void m3dCTRL_COLOR_NOISE::GetValue (m3dCOLOR *color)
{
   float coeff;
   m3dV  p;

   p.x = M3D_PI + freq * GetTimeCur();
   p.y = p.z = M3D_PI/2.f;
   coeff = minVal + ((maxVal - minVal) * (1.f + pNoise->GetValue(&p)) / 2.f);

   color->a = colorBase.a;
   color->r = colorBase.r * coeff;
   color->g = colorBase.g * coeff;
   color->b = colorBase.b * coeff;

   return;
}

//
// End-of-file M3D_CTRL.CPP
//
